package com.hrt.datasender.dataschedule;

import java.util.List;

import org.quartz.JobExecutionContext;

import com.btcode.common.MyLog;
import com.btcode.log.ILog;
import com.btcode.schedule.SingleInstanceJob;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcSendPlatformService;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.PacketModel;
import com.hrt.datasender.process.heartbeat.HeartbeatLoader;
import com.hrt.datasender.process.heartbeat.IHeartbeat;

/**
 * 定时从存储获取数据并打包发送
 */
public class HeatbeatScheduler extends SingleInstanceJob {

    @SuppressWarnings("unused")
    private ILog log;

    private List<BcSendPlatformModel> platformList = null;

    @Override
    public void initJob(JobExecutionContext arg0) {

        log = MyLog.getInstance().getLogger(getClass());
    }

    @Override
    public void myExecute(JobExecutionContext arg0) {

        if (platformList == null) {
            platformList = BcSendPlatformService.getInstance().getModelList();
        }

        for (BcSendPlatformModel platform : platformList) {

            String ip = platform.getIp();
            String port = platform.getPort();
            String mn = platform.getMn();

            String platformId = platform.getPlatformId();
            IHeartbeat heartbeat = HeartbeatLoader.getInstance().getHeartbeatByPlatformId(platformId);

            if (heartbeat == null) {
                continue;
            }

            String msg = heartbeat.getHeartBeatData(platform);

            if (msg == null) {
                continue;
            }

            PacketModel packetModel = new PacketModel();
            packetModel.setIp(ip);
            packetModel.setPort(port);
            packetModel.setMn(mn);
            packetModel.setMsg(msg);

            MqKit.productMsgToHeatBeatMQ(packetModel);

        }
    }

}
